package com.sneakxy.cloudbase.platform.bean.dto;

import java.util.ArrayList;
import java.util.List;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

import org.springframework.beans.BeanUtils;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.sneakxy.cloudbase.platform.bean.Home;
import com.sneakxy.cloudbase.platform.bean.ValidAdd;
import com.sneakxy.cloudbase.platform.bean.ValidEdit;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 首页信息DTO
 * @author 徐英
 * @see
 */
@Data
@ApiModel("首页信息")
@EqualsAndHashCode
@JsonIgnoreProperties(ignoreUnknown = true)
public class HomeDTO implements java.io.Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7768845007858448877L;

	@NotBlank(groups={ValidAdd.class, ValidEdit.class})
	@ApiModelProperty("菜单类型")
	private String type;

	@NotNull(groups={ValidEdit.class})
	@ApiModelProperty("是否登录:0、未登录，1、登录")
	private Boolean login;

	@NotBlank(groups={ValidAdd.class, ValidEdit.class})
	@ApiModelProperty("模板内容")
	private String template;

	@ApiModelProperty("创建时间")
	private String createtime;

	@ApiModelProperty("更新时间")
	private String updatetime;

	public static Home.HomeId toId(HomeDTO entity) {
		return new Home.HomeId(entity.getType(), entity.getLogin());
	}
	
	public static HomeDTO from(Home entity) {
		HomeDTO result = new HomeDTO();
		BeanUtils.copyProperties(entity, result);
		Home.HomeId id = entity.getId();
		if(id != null) {
			result.setType(id.getType());
			result.setLogin(id.getLogin());
		}
		return result;
	}
	
	public static List<HomeDTO> from(List<Home> list) {
		List<HomeDTO> result = new ArrayList<>(list.size());
		list.forEach(entity -> {
			result.add(from(entity));
		});
		return result;
	}
	
	public static Home to(HomeDTO entity) {
		Home result = new Home();
		BeanUtils.copyProperties(entity, result);
		Home.HomeId id = new Home.HomeId();
		id.setType(entity.getType());
		id.setLogin(entity.getLogin());
		result.setId(id);
		return result;
	}
	
	public static List<Home> to(List<HomeDTO> list) {
		List<Home> result = new ArrayList<>(list.size());
		list.forEach(entity -> {
			result.add(to(entity));
		});
		return result;
	}

}
